<include file="public@header" />
<style type="text/css">
.ulc{list-style:none; width:800px;}
.ulc li{width:190px;height:35px;float:left;}
</style>
</head>
<body>
<div class="wrap js-check-wrap">
	<ul class="nav nav-tabs">
        <li><a href="{:url('Store/index')}">店铺列表</a></li>
		<li class="active"><a href="{:url('Store/add')}">新增店铺</a></li>
    </ul>
    <form class="form-horizontal js-ajax-form margin-top-20" role="form" action="" method="post">
        <fieldset>
            <div class="tabbable">
                <div class="tab-content">
                    <div class="tab-pane active" id="A">                         
                        <div class="form-group">
                            <label  class="col-sm-2 control-label"><span class="form-required">*</span>店铺名称</label>
                            <div class="col-md-6 col-sm-10">
                                <input type="text" class="form-control" id="input-name" name="store_name" value="">
                            </div>
                        </div>                       
                        <div class="form-group">
                            <label  class="col-sm-2 control-label"><span class="form-required">*</span>店铺负责人</label>
                            <div class="col-md-6 col-sm-10">
                                <input type="text" class="form-control" id="input-name" name="principal" value="">
                            </div>
                        </div>
                        <!-- <div class="form-group">
                            <label  class="col-sm-2 control-label"><span class="form-required">*</span>身份证号</label>
                            <div class="col-md-6 col-sm-10">
                                <input type="text" class="form-control" id="input-name" name="idnumber" value="">
                            </div>
                        </div> -->
                        <!-- <div class="form-group">
                            <label  class="col-sm-2 control-label"><span class="form-required">*</span>手机</label>
                            <div class="col-md-6 col-sm-10">
                                <input type="text" class="form-control" id="input-name" name="mobile" value="">
                            </div>
                        </div> -->
                        <!-- <div class="form-group">
                            <label  class="col-sm-2 control-label"><span class="form-required">*</span>座机</label>
                            <div class="col-md-6 col-sm-10">
                                <input type="text" class="form-control" id="input-name" name="phone" value="">
                            </div>
                        </div> -->
                        <div class="form-group">
                            <label  class="col-sm-2 control-label"><span class="form-required">*</span>店铺地址</label>
                            <div class="col-md-6 col-sm-10">
                                <input type="text" class="form-control" id="input-name" name="address" value="">
                            </div>
                        </div>                     
                        <div class="form-group" >
                            <label  class="col-sm-2 control-label"><span class="form-required">*</span>管理员</label>
                            <div class="col-md-6 col-sm-10">
                                <select name="steer">
                                <option value="0">请选择</option>
                                    <foreach name="dudao_list" item="v">
                                    <option value="{$v.userid}">{:get_operator_data($v['userid'],'user_login')}</option>
                                    </foreach>
                                </select>
                            </div>
                        </div> 
                        <div class="form-group">
                            <label  class="col-sm-2 control-label"><span class="form-required">*</span>店铺类别</label>
                            <div class="col-md-6 col-sm-10">
                            	<select name="cat_store">
                                    <foreach name="cat_list" item="vo">
                                    <option value="{$vo.id}">{$vo.cat_name}</option>
                                    </foreach>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label  class="col-sm-2 control-label">
                            <span class="form-required">*</span>品牌列表</label>
                            <div class="col-md-6 col-sm-10">
                            	<select name="brandid" id='sel' >
                            	<option value="请选择" >请选择</option>
                                    <foreach name="brandList" item="vo">
                                    <option value="{$vo.id}" >{$vo.name}</option>
                                    </foreach>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label  class="col-sm-2 control-label">
                                <span class="form-required">*</span>省</label>
                            <div class="col-md-6 col-sm-10">
                                <select name="provincesid" id='provinces' >
                                    <option value="请选择" >请选择</option>
                                    <foreach name="provinces" item="vo">
                                        <option value="{$vo.provinceid}" >{$vo.province}</option>
                                    </foreach>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label  class="col-sm-2 control-label">
                                <span class="form-required">*</span>市</label>
                            <div class="col-md-6 col-sm-10">
                                <select name="cityid" id='city' >
                                    <option value="请选择" >请选择</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label  class="col-sm-2 control-label">
                                <span class="form-required">*</span>县</label>
                            <div class="col-md-6 col-sm-10">
                                <select name="areasid" id='areas' >
                                    <option value="请选择" >请选择</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label  class="col-sm-2 control-label"><span class="form-required">*</span>编辑商品</label>
                            <div class="col-md-6 col-sm-10">
                                <select name="special">
                                	<option value="0">关闭</option>
                                    <option value="1">开启</option>
                                </select>
                            </div>
                        </div>                                                  
                        <div class="form-group">
                                <label class="col-sm-2 control-label">
                                    <span class="form-required">*</span>选择店铺商品</label>
                                    <div>
                                    	<input  type="checkbox" id="selectAll">全选</input>
                                    	
                                    </div>
                                <div class="col-md-6 col-sm-10" id="addshop">
                                </div>
                            </div>
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button type="submit" class="btn btn-primary js-ajax-submit" data-refresh="1" onclick='sub()'>{:lang('SAVE')}</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </fieldset>
    </form>
</div>
<script type="text/javascript" src="__STATIC__/js/admin.js"></script>
<script type="text/javascript" src="/themes/admin_simpleboot3/public/assets/js/jquery-1.10.2.min.js" ></script>
<script type="text/javascript">	       
        $(function () {
            $('#sel').change(function () {
                var brandid = $('#sel option:selected').val();
                $.getJSON("{:url('Store/getGoods')}", { brandid: brandid }, function (json) {
                    // console.log(json);
                    var jsonObj = JSON.parse(json)
                    var jsonArr = []
                    for (var i = 0; i < jsonObj.length; i++) {
                        jsonArr[i] = jsonObj[i];
                    }
                    if (jsonArr.length!==0) {
                        var add = '';
                        /*add='<input  type="checkbox" id="selectAll">全选</input>';*/
                        add = '<ul class="ulc">';
                    $.each(jsonArr, function (k, v) {
                        // console.log(v);
                            add += '<li><input  type="checkbox" name="cat" value="' + v.g_id + '">' + v.g_goodsname +'</li>'
                            $('#addshop').html(add)
                    })
                    add += '</ul>';
                    } else {
                        $('#addshop').html('该分类没有商品！')
                    }
                    // 为每个动态添加的span添加样式
                    $('#addshop').find('span').css({
                        "display": "block",
                        "margin-right": "1rem",
                        "float": "left"
                    })

                });
            })
        })
        
   function sub(){
	    obj = document.getElementsByName("cat");
	    check_val = [];
	    for(k in obj){
	        if(obj[k].checked)
	            check_val.push(obj[k].value);
	    }
	    var id_str=check_val;
	    var store_name=$('input[name=store_name]').val();
	    var principal=$('input[name=principal]').val();
	    var address=$('input[name=address]').val();
	    var steer=$('select[name=steer]').val();
	    var cat_store=$('select[name=cat_store]').val();
	    var brandid=$('select[name=brandid]').val();
	    var special=$('select[name=special]').val();
	    var provincesid=$('#provinces option:selected').val();
	    if(provincesid == null)
        {
            alert('请选择省');
            return false;
        }
	    var cityid=$('#city option:selected').val();
       if(cityid == null)
       {
           alert('请选择市');
           return false;
       }
	    var areas=$('#areas option:selected').val();
       if(areas == null)
       {
           alert('请选择县');
           return false;
       }
	    $.getJSON("{:url('Store/addPost')}",{id_str:id_str,store_name:store_name,principal:principal,address:address,steer:steer,cat_store:cat_store,brandid:brandid,special:special,provinceid:provincesid,cityid:cityid,areaid:areas},function(json){
	    	alert(json.msg);
	    	window.location='{:url("store/index")}';
		});  
    }
    </script>    

<script>    
    // 复选框的全选和全不选
    $(function() {    	
    	 $('#selectAll ').click(function() {    		
            $(" :checkbox[name='cat']").attr("checked",true); // this指代的你当前选择的这个元素的JS对象
           
        });
    });
    $('#provinces').change(function () {
       var provinces=$('#provinces option:selected').val();
       $.post("{:url('store/city')}",{provinces:provinces},function (date) {
         var con="";
         $.each(date.msg,function(k,v){
             con +='<option value='+v.cityid+' >'+v.city+'</option>'
             $('#city').html(con);
         })
       })
    })
    $('#city').change(function(){
        var city=$('#city option:selected').val();
        if(city==0)
        {
            alert("请选择市级单位");
            return false;
        }
        $.post("{:url('store/areas')}",{city:city},function (date) {
            var con="";
            $.each(date.msg,function(k,v){
                con +='<option value='+v.areaid+' >'+v.area+'</option>'
                $('#areas').html(con);
            })
        })
    })
</script>      
</body>
</html>